using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Example.Business;
using NBusiness.Data;

namespace www
{
    public partial class Company : System.Web.UI.Page
    {
        private Example.Business.Company _company = null;


        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void DetailsView1_DataBound(object sender, EventArgs e)
        {

        }
        protected void CompanyDataSource_Select(object sender, NBusiness.Data.Web.SelectEntityEventArgs e)
        {
            int companyId;
            if (int.TryParse(Request["c"], out companyId) && companyId > 0)
            {
                if (_company == null)
                {
                    _company = Example.Business.Company.Fetch(companyId);
                }

                e.Entities = new EntityCollection(_company);
            }
        }
        protected void BeerDataSource_Select(object sender, NBusiness.Data.Web.SelectEntityEventArgs e)
        {
            int companyId;
            if (int.TryParse(Request["c"], out companyId) && companyId > 0)
            {
                Example.Business.Company c = Example.Business.Company.Fetch(companyId);
                e.Entities = c.Beers;
            }
        }
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                HyperLink styleLink = (HyperLink)e.Row.FindControl("StyleLink");
                Label alcoholLabel = (Label)e.Row.FindControl("AlcoholLabel");
                HyperLink availabilityLink = (HyperLink)e.Row.FindControl("AvailabilityLink");

                Beer b = (Beer)e.Row.DataItem;


                styleLink.Text = b.Style.Name;
                styleLink.NavigateUrl = "~/Search/ByStyle.aspx?s=" + b.BeerStyleId;

                alcoholLabel.Text = string.Format("{0:p}", b.Alcohol);

                availabilityLink.Text = b.Availability.Text;
                availabilityLink.NavigateUrl = "~/Search/ByAvailability.aspx?a=" + b.AvailabilityId.ToString();
            }
        }

        protected void GridView2_RowDataBound1(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                Example.Business.Address a = (Example.Business.Address)e.Row.DataItem;
                Label regionLabel = (Label)e.Row.FindControl("RegionLabel");
                regionLabel.Text = a.Region.Name;
            }
        }
        protected void AddressDataSource_Select(object sender, NBusiness.Data.Web.SelectEntityEventArgs e)
        {
            int companyId;
            if (int.TryParse(Request["c"], out companyId) && companyId > 0)
            {
                if (_company == null)
                {
                    _company = Example.Business.Company.Fetch(companyId);
                }

                e.Entities = _company.Addresses;
            }
        }
    }
}